#include "ogre/ExampleApplication.h"
#include "Terrain.h"

#define TERRAIN_SIZE 128
#define TEXRESOLUTION 128

#define LOWEST	0
#define LOW		1
#define HIGH	2
#define HIGHEST	3
#define TEXNUM	4

class Example1 : public ExampleApplication
{
	ITerrain* myDemoTerrain;
public:

	void createScene()
	{
		srand((unsigned)time(NULL));

		myDemoTerrain = new CFaultInfomationTerrain();
		myDemoTerrain->InitTerrainData("height128.raw", TERRAIN_SIZE);

		//config the light
		/*Ogre::Light* light = mSceneMgr->createLight("Light1");
		light->setType(Ogre::Light::LT_DIRECTIONAL);
		light->setDirection(Ogre::Vector3(1,-1,0));*/

		myDemoTerrain->m_texMgr->GenerateTexture(TEXRESOLUTION, myDemoTerrain);
		
		
		//Ogre::TextureManager::getSingleton().loadImage("tex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, *tex, Ogre::TEX_TYPE_2D, 0, 1.0f);
		///*Ogre::TextureManager::getSingleton().load("tex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
		//	Ogre::TEX_TYPE_2D, 0, 1.0f);*/
		
		Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create("myMaterialWithNoCulling",
			Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

		mat->setCullingMode(Ogre::CULL_NONE );
		mat->setSceneBlending(Ogre::SBT_ADD); 
		mat->getTechnique(0)->getPass(0)->createTextureUnitState("texP");
		Ogre::ManualObject* manual = mSceneMgr->createManualObject("MyTerrain");
		manual->estimateVertexCount((TERRAIN_SIZE-1)*(TERRAIN_SIZE-1)*6);
		manual->begin("myMaterialWithNoCulling", RenderOperation::OT_TRIANGLE_LIST);
		float fColor;
		float xU1,xU2,yV1,yV2;
		for(int iz = 0; iz < TERRAIN_SIZE - 1; ++iz)
		{
			for(int ix = 0; ix < TERRAIN_SIZE - 1 ; ++ix)
			{
				
				xU1 = (float)ix/(TERRAIN_SIZE - 1);
				xU2 = (float)(ix +1)/(TERRAIN_SIZE - 1);
				yV1 = (float)(iz)/(TERRAIN_SIZE - 1);
				yV2 = (float)(iz + 1)/(TERRAIN_SIZE - 1);
				

				fColor = myDemoTerrain->GetRawHeight(ix, iz);
				manual->position(ix, myDemoTerrain->GetScaleHeight(ix,iz), iz);
				manual->textureCoord(xU1, yV1);

				fColor = myDemoTerrain->GetRawHeight(ix, iz);
				manual->position(ix + 1, myDemoTerrain->GetScaleHeight(ix + 1,iz), iz);
				manual->textureCoord(xU2, yV1);

				fColor = myDemoTerrain->GetRawHeight(ix, iz);
				manual->position(ix, myDemoTerrain->GetScaleHeight(ix,iz + 1), iz + 1);
				manual->textureCoord(xU1, yV2);

				fColor = myDemoTerrain->GetRawHeight(ix, iz);
				manual->position(ix, myDemoTerrain->GetScaleHeight(ix,iz + 1), iz + 1);
				manual->textureCoord(xU1, yV2);

				fColor = myDemoTerrain->GetRawHeight(ix, iz);
				manual->position(ix + 1, myDemoTerrain->GetScaleHeight(ix + 1,iz), iz);
				manual->textureCoord(xU2, yV1);

				fColor = myDemoTerrain->GetRawHeight(ix, iz);
				manual->position(ix + 1, myDemoTerrain->GetScaleHeight(ix + 1,iz + 1), iz + 1);
				manual->textureCoord(xU2, yV2);
			}
		}
		manual->end();

		manual->convertToMesh("bruteForceTerrain");

		Ogre::Entity* terrainMesh = mSceneMgr->createEntity("bruteForceTerrain");

		mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(terrainMesh);
	}
	void createCamera()
	{
		mCamera = mSceneMgr->createCamera("MyCam1");
		mCamera->setPosition(0,200,200);
		mCamera->lookAt(0,0,0);
		mCamera->setNearClipDistance(5);
		//mCamera->setPolygonMode(PolygonMode::PM_WIREFRAME);
		
	}
};


int main(void)
{
	Example1 app;
	app.go();
	return 0;
}
